package com.chixing.service.impl;

import com.chixing.mapper.FlowMapper;
import com.chixing.pojo.Flow;
import com.chixing.service.FlowService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author Wesley
 * @version 1.0
 * @date 2021/10/5 15:31
 */
@Service
public class FlowServiceImpl implements FlowService {
    @Autowired
    private FlowMapper flowMapper;
    @Override
    public ServerResponse getById(Integer flowId) {
        Flow flow = flowMapper.selectByPrimaryKey(flowId);
        if(flow != null)
            return ServerResponse.getSuccess(flow);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse save(Long flowNumber,Integer custId, Integer myOrderId, Float orderPrice) {
        Flow flow = new Flow();

        //利用UUID生成订单号
        String[] chars = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-","");
        for (int j=0;j<16;j++) {
            String str=uuid.substring(j*2,j*2+2);
            int par=Integer.parseInt(str,24);
            shortBuffer.append(chars[par % 10]);
        }
        Long flowNum = Long.valueOf(shortBuffer.toString());

        //利用UUID生成回执编号
        String[] chars1 = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A","B","C","D","E","F","G",
                "H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        StringBuffer shortBuffer1 = new StringBuffer();
        String uuid1 = UUID.randomUUID().toString().replace("-","");
        for (int j=0;j<6;j++) {
            String str=uuid1.substring(j*4,j*4+4);
            int par=Integer.parseInt(str,36);
            shortBuffer1.append(chars1[par % 36]);
        }
        String receiptNumber  = shortBuffer1.toString();
        flow.setFlowNumber(flowNum);
        flow.setCustId(custId);
        flow.setMyOrderId(myOrderId);
        flow.setPayTime( new Date());
        flow.setPayAmount(orderPrice);
        flow.setReceiptNumber(receiptNumber);
        flow.setStatus(0);
        int insert = flowMapper.insert(flow);
        if(insert > 0)
            return ServerResponse.saveSuccess(flow);
        return ServerResponse.saveFail(null);
    }


}
